﻿//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：Controller
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/22 15:37:09
*******************************************************************
* Copyright @ henrychang 2018. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using System;
using System.Collections.Generic;

namespace TouchFish.PureMVC
{
    public class Controller : IController
    {
        protected Dictionary<int, Func<ICommand>> commandMap;
        protected int multitonKey = 0;

        protected IView view;

        public Controller(int key)
        {
            IController controller;
            if (instanceMap.TryGetValue(key, out controller) && multitonKey > 0)
                throw new Exception(ERROR_MSG + "  " + key);
            multitonKey = key;
            instanceMap.TryAdd(multitonKey, this);
            commandMap = new Dictionary<int, Func<ICommand>>();
            InitializeController();
        }

        protected virtual void InitializeController()
        {
            //view = View.GetInstance(multitonKey, () => new View(multitonKey));
        }

        public virtual void ExecuteCommand(INotification notification)
        {
            Func<ICommand> commandClassRef;
            if (commandMap.TryGetValue(notification.NotifityId, out commandClassRef))
            {
                ICommand commandInstance = commandClassRef();
                commandInstance.InitializeNotifier(multitonKey);
                commandInstance.Execute(notification);
            }
        }

        public virtual void RegisterCommand(int notificationId, Func<ICommand> commandClassRef)
        {
            Func<ICommand> tempCommandClassRef;

            if (commandMap.TryGetValue(notificationId, out tempCommandClassRef) == false)
            {
                view.RegisterObserver(notificationId, new Observer(ExecuteCommand, this));
            }
            commandMap[notificationId] = commandClassRef;
        }

        public virtual void RemoveCommand(int notificationId)
        {
            if (commandMap.Remove(notificationId))
            {
                view.RemoveObserver(notificationId, this);
            }
        }

        public virtual bool HasCommand(int notificationId)
        {
            return commandMap.ContainsKey(notificationId);
        }

        #region Static

        public static IController GetInstance(int key)
        {
            IController controller;
            if (!instanceMap.TryGetValue(key, out controller))
            {
                controller = new Controller(key);
            }
            return controller;
        }

        public static void RemoveController(int key)
        {
            instanceMap.Remove(key);
        }

        protected static Dictionary<int, IController> instanceMap = new Dictionary<int, IController>();

        protected const string ERROR_MSG = "已经创建过 Controller Key值的实例";

        #endregion Static
    }
}